package com.bw.paper.mvp.view.user;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bw.paper.App;
import com.bw.paper.R;
import com.bw.paper.mvp.base.model.BaseEntity;
import com.bw.paper.mvp.base.view.BaseActivity;
import com.bw.paper.mvp.contract.UserDetailsContract;
import com.bw.paper.mvp.di.DaggerUserDetailsComponent;
import com.bw.paper.mvp.di.UserDetailsModules;
import com.bw.paper.mvp.model.entity.ResponseUserEntity;
import com.bw.paper.mvp.model.entity.UserEntity;
import com.bw.paper.mvp.presenter.UserDetailsPresenter;
import com.bw.paper.network.LoadImage;
import com.bw.paper.utils.LogUtils;
import com.bw.paper.widget.PhotoPop;
import com.bw.paper.widget.SexPop;

import java.util.HashMap;
import java.util.Map;

import androidx.annotation.Nullable;

public class UserDetailsActivity extends BaseActivity<UserDetailsPresenter> implements UserDetailsContract.IUserDetailsView ,
        View.OnClickListener, SexPop.SexCallBack {

    private ImageView head_back_img,user_head_img;
    private RelativeLayout user_head_relative,user_nick_relative,user_sex_relative;
    private TextView user_nick_tv,user_sex_tv;
    private PhotoPop photoPop;
    private SexPop sexPop;

    @Override
    public View addStatusView() {
        return f(R.id.user_details_root);
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_userdetails;
    }

    @Override
    public void initView() {
        photoPop = new PhotoPop(this);
        sexPop = new SexPop(this,this);
        head_back_img = f(R.id.head_back_img);
        user_head_img = f(R.id.user_head_img);
        user_head_relative = f(R.id.user_head_relative);
        user_nick_relative = f(R.id.user_nick_relative);
        user_sex_relative = f(R.id.user_sex_relative);
        user_nick_tv = f(R.id.user_nick_tv);
        user_sex_tv = f(R.id.user_sex_tv);
        head_back_img.setOnClickListener(this);
        user_head_relative.setOnClickListener(this);
        user_nick_relative.setOnClickListener(this);
        user_sex_relative.setOnClickListener(this);
    }

    @Override
    public void initData() {
        UserEntity entity = App.getInstance().getDaoSession().queryBuilder(UserEntity.class)
                .list().get(0);
        Map<String,Object>map = new HashMap<>();
        map.put("user_id",entity.user_id);
        p.requestUserDetails(map);
    }

    @Override
    public void inject() {
        DaggerUserDetailsComponent.builder().userDetailsModules(new UserDetailsModules(this))
                .build().inject(this);
    }

    @Override
    public void refreshView(BaseEntity entity) {
        if (entity instanceof ResponseUserEntity) {
            App.getInstance().getDaoSession().deleteAll(UserEntity.class);
            ResponseUserEntity responseUserEntity = (ResponseUserEntity) entity;
            UserEntity userEntity = responseUserEntity.getValues();
            App.getInstance().getDaoSession().insert(userEntity);
            updateUI(userEntity);
        }
    }

    private void updateUI(UserEntity userEntity){
        if (!userEntity.user_img.isEmpty())
            LoadImage.loadCircleImg(userEntity.user_img,user_head_img);
        user_nick_tv.setText(userEntity.user_name);
        String sex_tv = "";
        if (userEntity.user_sex.equals(""))
            sex_tv = "保密";
        if (userEntity.user_sex.equals("0"))
            sex_tv = "男";
        if (userEntity.user_sex.equals("1"))
            sex_tv = "女";
        user_sex_tv.setText(sex_tv);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
            if (!data.getStringExtra("nick").isEmpty()){
                Map<String,Object>map = new HashMap<>();
                UserEntity entity = App.getInstance().getDaoSession().queryBuilder(UserEntity.class)
                        .list().get(0);
                map.put("user_id",entity.user_id);
                map.put("user_img",entity.user_img);
                map.put("user_integral",entity.user_integral);
                map.put("user_name",data.getStringExtra("nick"));
                map.put("user_phone",entity.user_phone);
                map.put("user_pwd",entity.user_pwd);
                map.put("user_sex",entity.user_sex);
                p.requestUpdateUser(map);
            }else {
                String path = data.getStringExtra("path");
                LogUtils.show("UserDetailsActivity:" + path);
                p.uploadHead(path);
            }
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (R.id.head_back_img==id)
            finish();
        else if (R.id.user_head_relative==id){
            photoPop.show(getWindow().getDecorView().getRootView());
        } else if (R.id.user_nick_relative==id){
            Intent intent = new Intent(this,UserNameActivity.class);
            startActivityForResult(intent,100);
        } else if (R.id.user_sex_relative==id){
            sexPop.show(getWindow().getDecorView().getRootView());
        }
    }

    @Override
    public void sexCall(int sexCode) {
        Map<String,Object>map = new HashMap<>();
        UserEntity entity = App.getInstance().getDaoSession().queryBuilder(UserEntity.class)
                .list().get(0);
        map.put("user_id",entity.user_id);
        map.put("user_img",entity.user_img);
        map.put("user_integral",entity.user_integral);
        map.put("user_name",entity.user_name);
        map.put("user_phone",entity.user_phone);
        map.put("user_pwd",entity.user_pwd);
        map.put("user_sex",""+sexCode);
        p.requestUpdateUser(map);
    }
}
